﻿using MediatR;
using SheepCultivation.Common.Domain.SheepBasicInformation;
using SheepCultivation.Common.Dto;
using SheepCultivation.Common.Infrastructure.Interface;
using SheepCultivation.Enum;

namespace SheepCultivation.API.AddliCation.SheepManagement
{
    public class DeathManagementListHandler : IRequestHandler<DeathManagementListCommon, Result>
    {
        #region 仓储
        /// <summary>
        /// 基本信息仓储
        /// </summary>
        public readonly IBaseRepository<BasicInformation> BasicInformationRepository;
        /// <summary>
        /// 日志
        /// </summary>
        public readonly ILogger<DeathManagementListHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="BasicInformationRepository">基本信息</param>
        /// <param name="logger">日志</param>
        public DeathManagementListHandler(IBaseRepository<BasicInformation> BasicInformationRepository, ILogger<DeathManagementListHandler> logger)
        {
            this.BasicInformationRepository = BasicInformationRepository;
            this.logger = logger;
        }
        #endregion

        #region 下拉框羊只耳号
        /// <summary>
        /// 下拉框羊只耳号
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(DeathManagementListCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonEnumCode.error };
            //异常处理
            try
            {
                logger.LogInformation("下拉框羊只耳号成功!");
                var list = BasicInformationRepository.GetAll().Where(x => x.SheepTypeId == request.sheepTypeId).ToList();

                result.Code = (int)CommonEnumCode.success;
                result.Data = list;
                result.Msg = "下拉框羊只耳号成功";
            }
            catch (Exception ex)
            {
                logger.LogError("下拉框羊只耳号成功失败!" + ex.Message);
                result.Msg = "下拉框羊只耳号成功失败";
            }
            return result;
        }
        #endregion
    }
}
